package com.authsphere.security.configurer.verifycode.filter;

import com.authsphere.common.constant.Constants;
import com.authsphere.security.common.AbstractRequestBodyAuthenticationFilter;
import com.authsphere.security.configurer.verifycode.SmsVerificationCodeAuthenticationToken;
import com.authsphere.security.configurer.verifycode.entity.VerificationCodeEntity;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/11 11:32
 **/
public class VerificationCodeRequestBodyAuthenticationFilter extends AbstractRequestBodyAuthenticationFilter<VerificationCodeEntity> {

    private final static String DEFAULT_PROCESS_URL = "/sms/login";
    private final static String DEFAULT_MODULE_TYPE = "sms_login";

    public VerificationCodeRequestBodyAuthenticationFilter() {
        super(DEFAULT_MODULE_TYPE, DEFAULT_PROCESS_URL, VerificationCodeEntity.class);
    }

    public VerificationCodeRequestBodyAuthenticationFilter(String moduleType) {
        super(moduleType, DEFAULT_PROCESS_URL, VerificationCodeEntity.class);
    }
    @Override
    protected Authentication buildAuthenticationToken(HttpServletRequest request, HttpServletResponse response, VerificationCodeEntity requestJsonBody) {
        return new SmsVerificationCodeAuthenticationToken(requestJsonBody.getUsername(), requestJsonBody.getVerifyCode());
    }

    @Override
    protected void beforeAuthentication(HttpServletRequest request, HttpServletResponse response, Object data) {
        super.beforeAuthentication(request, response, data);
        request.setAttribute(Constants.OPERATE_NAME, "用户验证码登录");
    }
}
